package com.lych.server.controller;

import com.lych.model.mongo.Movement;
import com.lych.model.vo.MovementsVo;
import com.lych.model.vo.PageResult;
import com.lych.model.vo.VisitorsVo;
import com.lych.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.Path;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 动态-发布
     * @param movement
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementsService.movements(movement, imageContent);

        return ResponseEntity.ok(null);
    }

    /**
     * 我的动态
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity all(Long userId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementsService.all(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询好友动态
     * @return
     */
    @GetMapping
    public ResponseEntity findMovements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementsService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementsService.recommend(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 根据id查询动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId){
        MovementsVo vo = movementsService.movementFindById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity idLike(@PathVariable("id") String movementId){
        Integer likeCount = movementsService.idLike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity idDisLike(@PathVariable("id") String movementId){
        Integer disLikeCount = movementsService.idDisLike(movementId);
        return ResponseEntity.ok(disLikeCount);
    }

    /**
     * 喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity idLove(@PathVariable("id") String movementId){
        Integer idLoveCount = movementsService.idLove(movementId);
        return ResponseEntity.ok(idLoveCount);
    }

    /**
     * 动态取消喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity idUnlove(@PathVariable("id") String movementId){
        Integer idUnloveCount = movementsService.idUnlove(movementId);
        return ResponseEntity.ok(idUnloveCount);
    }

    /**
     * 谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> list = movementsService.visitors();
        return ResponseEntity.ok(list);
    }
}
